export default {
  onParameterUpdate: function(self, ctx, key, value) {
    if (key === 'Alarm' && value === true && self.scriptObject) {
      this.mixer.timeScale = 0;
    }
    if (key === 'Alarm' && value === false && self.scriptObject) {
      this.mixer.timeScale = 1;
    }
    if (key === 'pStart' && (value == 1 || value == '1') && !self.scriptObject) {
      this.onScriptInit(self, ctx);
    }
  },
  update: function(delta, self, ctx) {
    if (self.scriptObject) {
      this.onScriptUpdate(self, ctx, delta);
    }
  },
  onScriptInit: function(self, ctx) {
    const ANIMATION = ['car|Take 001|BaseLayer','polySurface5|Take 001|BaseLayer','polySurface6|Take 001|BaseLayer'];
    const DURATION = 10;
    const AnimationMixer = HaloEngine.Three.AnimationMixer;
    const LoopOnce = HaloEngine.Three.LoopOnce;
    const mixer = new AnimationMixer(self.group);

    // 创建并初始化动画实例
    ANIMATION.forEach(name => {
      const clip = self.getAnimation(name);
      if (clip) {
        const action = mixer.clipAction(clip);
        action.clampWhenFinished = true;
        action.setLoop(LoopOnce, 1);
        action.setDuration(DURATION);
        action.play();
      }
    });

    // 完成事件监听, 销毁当前对象
    mixer.addEventListener('finished', () => {
      if (self && self.parent) {
        self.parent.delete(self.uuid);
      }
    });

    // 保存 Script 数据
    self.scriptObject = {
      mixer: mixer
    };
  },
  onScriptUpdate: function(self, ctx, delta) {
    self.scriptObject.mixer.update(delta);
  },
}
